package logic.weapons;

import logic.ships.Ship;
import logic.slots.NodeSlot;

import com.jme.math.Vector3f;
import com.jme.scene.TriMesh;

public class WeaponSlot extends NodeSlot {
	
	private static final long serialVersionUID = 1L;
	
	protected TriMesh socket, front;
	protected Vector3f weaponLoc, weaponFrontLoc;
	protected Weapon weapon;
	protected float weaponSize;
	
	public WeaponSlot(TriMesh socket, TriMesh front, Ship ship) {
		super("WeaponSlot", ship, socket, front);
		this.socket = socket;
		this.front = front;
	}
	
	public void setWeapon(Weapon weapon) {
		this.weapon = weapon;
		updateWeapon();
	}
	
	@Override
	public void checkSlots() {
		weaponLoc = getLocalPos(socket);
		weaponFrontLoc = getLocalPos(front);
		weaponSize = getMaxSizeOf(socket.getWorldBound());
		Vector3f dir = weaponFrontLoc.subtract(weaponLoc).mult(weaponSize);
		weaponSize /= 3f;
		weaponFrontLoc.addLocal(dir);
		updateWeapon();
		
		socket.removeFromParent();
		front.removeFromParent();
		removeFromParent();
	}
	
	protected void updateWeapon() {
		if(weapon != null && weaponLoc != null) {
			weapon.getLocalTranslation().set(weaponLoc);
			weapon.setDefaultScale(weaponSize);
			weapon.setPointLocation(weaponFrontLoc);
		}
	}
	
	public Vector3f getWeaponLocation() { return weaponLoc; }
	
	public Vector3f getWeaponFrontLoc() { return weaponFrontLoc; }
}